package com.superyee.commonlib.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.superyee.commonlib.logic.BaseLogic;
import com.superyee.commonlib.observer.UiObserver;
import com.superyee.commonlib.widgets.LoadingDialog;

import static android.content.Context.INPUT_METHOD_SERVICE;


/**
 * 版权所有 2017 金泰节能科技有限公司,保留所有版权。<br>
 * 项目名：	农信 - Android客户端<br>
 * 项目描述：Activity基础类，提供一些公共的能力实现和一些抽象的方法。
 * 作者： jingchao 日期：2017-11-15
 */
public abstract class BaseFragment extends Fragment implements UiObserver {

    public FragmentActivity baseActivity;
    private LoadingDialog mLoadingDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseActivity = getActivity();
    }

    private int action;

    public int getAction() {
        if (action == 0) {
            action = BaseActivity.baseAction.incrementAndGet();
        }
        return action;
    }

    public int makeAction(int logicAction) {
        return BaseLogic.makeAction(getAction(), logicAction);
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        addListener();
        return inflater.inflate(getLayoutId(), container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    protected abstract int getLayoutId();

    /**
     * 初始化Fragment中的布局文件
     */
    protected abstract void initView(View view, Bundle savedInstanceState);

    /**
     * 初始化Fragment中用到的数据
     */
    public abstract void initData();

    protected abstract void addListener();

    protected abstract void removeListener();


    @Override
    public void onDestroyView() {
        removeListener();
        closeLoadingDialog();
        super.onDestroyView();
    }


    /**
     * 显示软键盘
     *
     * @param context
     * @param inputView
     */
    public void showInputMethod(Context context, EditText inputView) {
        if (inputView != null) {
            inputView.requestFocus();
            inputView.setSelection(inputView.getText().length());
            InputMethodManager imm = (InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE);
            imm.showSoftInput(inputView, InputMethodManager.SHOW_FORCED);
        }
    }

    public void hideInputMethod() {
        if (baseActivity == null) {
            return;
        }
        View view = baseActivity.getWindow().peekDecorView();
        if (view != null && view.getWindowToken() != null) {
            InputMethodManager imm = (InputMethodManager)
                    baseActivity.getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    protected void showLoadingDialog() {
        showLoadingDialog(null);
    }

    protected void showLoadingDialog(String msg) {
        if (mLoadingDialog == null) {
            mLoadingDialog = LoadingDialog.createLoadingDialog(baseActivity);
        }
        mLoadingDialog.setMessage(msg);
        mLoadingDialog.show();
    }

    protected void closeLoadingDialog() {
        LoadingDialog.closeDialog(mLoadingDialog);
    }

}
